<?php

namespace Admin\Controller;

class TopicController extends AdminController {

    public function index() {


        $field = "*";
        $order = "create_time desc";

        $where['is_del'] = 0;
        $M = M('Topic');

        $list = $this->lists($M, $where, $order, $field);

        if ($list) {
            foreach ($list as &$val) {
                $val['follow_num'] = M('TopicFollow')->where(['topic_id' => $val['topic_id']])->count();
                $val['post_num'] = M('Post')->where(['topic_id' => $val['topic_id']])->count();
            }
            unset($val);
        }

        $list = $this->arraySequence($list, 'follow_num');

        $this->assign('_list', $list);
        $this->assign('meta_title', '话题管理');
        $this->display();
    }

    public function info() {
        $id = I('id', 0, 'intval');

        if ($id == 0) {
            $this->error('错误的ID');
        }

        $info = M('Topic')->where(['topic_id' => $id])->find();

        if (empty($info)) {
            $this->error('找不到话题数据');
        }


        $postWhere['p.topic_id'] = $info['topic_id'];
        $postWhere['p.is_del'] = 0;


        $postM = M()->table('ot_post p')
                ->join('left join ot_users u on p.user_id = u.user_id');

        $field = "p.post_id,p.title,p.post_type,p.topic_id,p.user_id,p.content,p.img_list,p.video_url,p.create_time,u.head_imgurl,u.nick_name,p.is_system";
        $list = $this->lists($postM, $postWhere, 'p.create_time desc', $field);


        $info['post_list'] = [];
        if ($list) {
            foreach ($list as &$val) {
                if ($val['img_list']) {
                    $img_list = explode(',', $val['img_list']);
                    $val['img_url'] = $img_list[0];
                } else {
                    $val['img_url'] = '';
                }
                $val['comment_num'] = M('PostComment')->where(['post_id' => $val['post_id']])->count();
            }
            unset($val);


            $info['post_list'] = $list;
        }


        $this->assign('_list', $list);
        $this->assign('data', $info);
        $this->display();
    }

    public function add() {
        $id = I('id', 0, 'intval');
        if (IS_POST) {

            $data['topic_name'] = I('topic_name');
            $data['topic_pic'] = I("topic_pic");

            if (empty($data['topic_name'])) {
                $this->error('请输入话题标题!');
                exit;
            }

            if (empty($data['topic_pic'])) {
                $this->error('请上传封面图!');
                exit;
            }

            $data['topic_content'] = I("topic_content");


            if ($id == 0) {
                $res = M('Topic')->add($data);
            } else {
                $res = M('Topic')->where(array('topic_id' => $id))->save($data);
            }

            $this->success('操作成功', U('index'));

//            if ($res) {
//                $this->success('操作成功', U('index'));
//            } else {
//                $this->error('操作失败');
//            }
        } else {
            $data = M('Topic')->where(['topic_id' => $id])->find();

            $this->assign('data', $data);
            $this->display();
        }
    }

    public function getInfo() {
        $fid = I('fid', 0, 'intval');

        $list = M('Area')->where(array('fid' => $fid))->select();
        $this->ajaxReturn($list);
    }

    public function changeStatus() {
        $s = I('s', 0, 'intval');

        $id = I('id', 0, 'intval');

        if ($id == 0) {
            $this->error('错误的ID');
        }

        $data['status'] = $s;

        $res = M('Dog')->where(['dog_id' => $id])->save($data);

        if ($res) {
            $this->success();
        } else {
            $this->error();
        }
    }

    public function del() {
        $id = I('id', 0, 'intval');

        if ($id == 0) {
            $this->error('请选择正确的话题!');
            exit;
        }

        $res = M('Topic')->where(array('topic_id' => $id))->save(['is_del' => 1, 'update_time' => date('Y-m-d H:i:s', time())]);

        if ($res) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 二维数组根据字段进行排序
     * @params array $array 需要排序的数组
     * @params string $field 排序的字段
     * @params string $sort 排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
     */
    function arraySequence($array, $field, $sort = 'SORT_DESC') {
        $arrSort = array();
        foreach ($array as $uniqid => $row) {
            foreach ($row as $key => $value) {
                $arrSort[$key][$uniqid] = $value;
            }
        }
        array_multisort($arrSort[$field], constant($sort), $array);
        return $array;
    }

}
